<?php
class Scenery_enshrine_model extends CI_Model
{
    private $_table  = 'scenery_enshrine';
    private $_table1 = 'scenery_base';
    private $_table2 = 'scenery_info';
    private $_table3 = 'goods_base';

    public function findByEnshrine($params=array())
    {
        $this->db->where('uid', $params['uid']);
        $this->db->where('sid', $params['sid']);
        return $this->db->get($this->_table);
    }
    
    public function saveEnshrine($params=array())
    {
        $data = array(
            'sid'        => $params['sid'],
            'uid'        => $params['uid'],
            'created_at' => date('Y-m-d H:i:s'),
        );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }
    
    /**
     * 单个删除
     */
    public function deleteByEnshrine($params=array())
    {
        $this->db->where('uid', $params['uid']);
        $this->db->where('sid', $params['sid']);
        return $this->db->delete($this->_table);
    }
    
    /**
     * 景区收藏总页数
     */
    public function sceneryEnshrineTotal($params=array())
    {
        $this->db->select('enshrine_id');
        $this->db->from($this->_table. ' AS scenery_enshrine');
        $this->db->join($this->_table1.' AS scenery_base', 'scenery_base.sid=scenery_enshrine.sid', 'INNER');
        $this->db->join($this->_table2.' AS scenery_info', 'scenery_base.sid=scenery_info.sid', 'INNER');
        // $this->db->join($this->_table3.' AS goods_base', 'goods_base.sid=scenery_base.sid AND goods_base.is_check=2 AND goods_base.bit_opt&1=1', 'INNER');
        $this->db->where('scenery_enshrine.uid', $params['uid']);
        return $this->db->count_all_results();
    }

    /**
     * 景区收藏列表
     */
    public function sceneryEnshrineList($params=array(), $count, $start = 0)
    {
        $this->db->select('scenery_enshrine.enshrine_id, MIN(goods_base.shop_price) AS shop_price, SUM(goods_base.sold_number) AS sales_volume,
                        enshrine_id, scenery_base.sid, scenery_base.bit_state, name_hanzi, star_rank, scenery_info.minipics');
        $this->db->from($this->_table. ' AS scenery_enshrine');
        $this->db->join($this->_table1.' AS scenery_base', 'scenery_base.sid=scenery_enshrine.sid', 'INNER');
        $this->db->join($this->_table2.' AS scenery_info', 'scenery_base.sid=scenery_info.sid', 'INNER');
        $this->db->join($this->_table3.' AS goods_base', 'goods_base.sid=scenery_base.sid AND goods_base.is_check=2 AND goods_base.bit_opt&1=1', 'LEFT');
        $this->db->where('scenery_enshrine.uid', $params['uid']);
        $this->db->group_by('scenery_base.sid');
        $this->db->order_by('enshrine_id', 'DESC');
        $this->db->limit($start, $count);
        return $this->db->get();
    }
    
    /**
     * 批量删除
     */
    public function deleteEnshrine($ids)
    {
        $this->db->where_in('enshrine_id', $ids);
        return $this->db->delete($this->_table);
    }
}